#include "init_admin.hpp"

Init_Admin::Init_Admin()
{
  string file(FILE_ADMIN);
  FILE* config=fopen(file.c_str(),"r");
  char* line=NULL;
  size_t n;
  string information[4];
  unsigned int get_info(0);

  for(unsigned int cpt(0) ; cpt < 4 ; ++cpt)
    information[cpt]="";

  if(config != NULL)
    {
      cout << "OPEN FILE" << endl;

      fseek(config,0,SEEK_SET);

      while(getline(&line,&n,config) != -1)
	{
	  if(line[0] != '#' && line[0] != '\n')
	    {
	      information[get_info]=line;
	      ++get_info;
	    }

	  free(line);
	  line=NULL;
	  n=0;

	  if(get_info == 4)
	    {
	      if(admin.head->values.name == "")
		{
		  admin.head->values.priority=information[3];
		  admin.head->values.name=information[2];
		  admin.head->values.gui=information[1];
		  admin.head->values.ip=information[0];
		}
	      else
		{
		  Player p(information[0],information[1],information[2],information[3]);
		  admin.add_element(p);
		}

	      get_info=0;
	    }
	}

      free(line);
      line=NULL;
      fclose(config);
      config=NULL;
    }

}

void Init_Admin::print_element()
{
  Node<Player>* read=admin.head;

  while(read != NULL)
    {
      read->values.print_player();
      read=read->next;
    }
}
